<%@ page import="java.util.ArrayList" %>
<%@ page import="bean.User" %>
<%@ page import="bean.Article" %><%--
  Created by IntelliJ IDEA.
  User: 蒋缇
  Date: 2020/10/8
  Time: 20:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    ArrayList<User> users = (ArrayList<User>) request.getSession().getAttribute("administratorUsers");
    ArrayList<Article> articles = (ArrayList<Article>) request.getSession().getAttribute("administratorArticles");
    ArrayList<Article> commands = (ArrayList<Article>) request.getSession().getAttribute("administratorCommands");
%>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="css/allStyle.css">
</head>
<body>
<div class="row">
    <div class="col-lg-1">
        <ul class="list-group">
            <li class="list-group-item" id="0" onclick="switchPanel('users',0)" style="background-color: #E83737">用户
            </li>
            <li class="list-group-item" id="1" onclick="switchPanel('articles',1)" style="background-color: white">博客
            </li>
            <li class="list-group-item"><a href="index.jsp">首页</a></li>
        </ul>
    </div>
    <div class="col-lg-11">
        <%--            用户列表--%>
        <div class="show" id="users">
            <div class="panel panel-default">
                <div class="panel-body">
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th>头像</th>
                            <th>账号</th>
                            <th>密码</th>
                            <th>姓名</th>
                            <th>性别</th>
                            <th>签名</th>
                            <th>昵称</th>
                            <th>生日</th>
                            <th>电话</th>
                            <th>邮箱</th>
                            <th>地址</th>
                        </tr>
                        </thead>
                        <tbody>
                        <%
                            for (int i = 0; i < users.size(); i++) {
                                User user = users.get(i);
                        %>
                        <tr>
                            <td><img
                                    src="<%=user.getHeadPortrait().equals("")?"images/defaultHeadPortrait.jpg":user.getHeadPortrait()%>"
                                    alt="无法加载" class="img-circle" width="30px" height="30px"></td>
                            <td><%=user.getAccount()%>
                            </td>
                            <td><%=user.getPsw()%>
                            </td>
                            <td><%=user.getName()%>
                            </td>
                            <td><%=user.getSex()%>
                            </td>
                            <td><%=user.getSign()%>
                            </td>
                            <td><%=user.getScreenName()%>
                            </td>
                            <td><%=user.getBirthday()%>
                            </td>
                            <td><%=user.getPhone()%>
                            </td>
                            <td><%=user.getEmail()%>
                            </td>
                            <td><%=user.getAddress()%>
                            </td>
                            <td>
                                <button class="btn btn-info"
                                        onclick="getUserModal('<%=user.getHeadPortrait()%>','<%=user.getAccount()%>','<%=user.getPsw()%>','<%=user.getName()%>',
                                                '<%=user.getSex()%>','<%=user.getSign()%>','<%=user.getScreenName()%>','<%=user.getBirthday()%>',
                                                '<%=user.getPhone()%>','<%=user.getEmail()%>','<%=user.getAddress()%>','<%=user.getAge()%>')">
                                    修改
                                </button>
                            </td>
                            <td>
                                <button class=" btn btn-danger" onclick="deleteUser(<%=user.getAccount()%>)">删除</button>
                            </td>
                        </tr>
                        <%
                            }
                        %>
                        <%--                            模态框--%>
                        <div class="modal fade" tabindex="-1" role="dialog" id="userModel"
                             aria-labelledby="gridSystemModalLabel">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="gridSystemModalLabel">修改用户信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <img id="m1" src="" alt="无法加载" class="img-circle" width="40px" height="40px">
                                        <hr>
                                        <span>账号</span><input id="u1" class="form-control" value="" readonly>
                                        <span>密码</span><input id="u2" class="form-control" value="">
                                        <span>姓名</span><input id="u3" class="form-control" value="">
                                        <span>性别</span><input id="u4" class="form-control" value="">
                                        <span>年龄</span><input id="u5" class="form-control" value="">
                                        <span>签名</span><input id="u6" class="form-control" value="">
                                        <span>昵称</span><input id="u7" class="form-control" value="">
                                        <span>生日</span><input id="u8" class="form-control" value="">
                                        <span>电话</span><input id="u9" class="form-control" value="">
                                        <span>邮箱</span><input id="u10" class="form-control" value="" readonly>
                                        <span>地址</span><input id="u11" class="form-control" value="">
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                        <button type="button" class="btn btn-primary" onclick="updateUser()">保存修改
                                        </button>
                                    </div>
                                </div><!-- /.modal-content -->
                            </div><!-- /.modal-dialog -->
                        </div><!-- /.modal -->
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <%--    博客列表--%>
        <div class="hidden" id="articles">
            <div class="panel panel-default">
                <div class="panel-body">
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th>标题</th>
                            <th>作者</th>
                            <th>账号</th>
                            <th>时间</th>
                            <th>类型</th>
                            <th>分栏</th>
                            <th>分类</th>
                            <th>浏览数</th>
                            <th>评论数</th>
                            <th>喜爱度</th>
                        </tr>
                        </thead>
                        <tbody>
                        <%
                            for (int i = 0; i < articles.size(); i++) {
                                Article article = articles.get(i);
                        %>
                        <tr>
                            <td><%=article.getTitle()%>
                            </td>
                            <td><%=article.getUser()%>
                            </td>
                            <td><%=article.getId()%>
                            </td>
                            <td><%=article.getTime()%>
                            </td>
                            <td><%=article.getType()%>
                            </td>
                            <td><%=article.getSubfield()%>
                            </td>
                            <td><%=article.getCategory()%>
                            </td>
                            <td><%=article.getViews()%>
                            </td>
                            <td><%=article.getCommentCount()%>
                            </td>
                            <td><%=article.getLikingDegree()%>
                            </td>
                            <td><%=article.getLike()%>
                            </td>
                            <td>
                                <button class="btn btn-default" onclick="browseBlog(<%=article.getId()%>)">查看</button>
                            </td>
                            <%
                                int flag = 0;
                                for (int j = 0; j < commands.size(); j++) {
                                    if (article.getId() == commands.get(j).getId()) {
                                        flag = 1;
                                        break;
                                    }
                                }
                            %>
                            <%
                                if (flag == 1) {
                            %>
                            <td>
                                <button class="btn btn-warning" onclick="cancelCommand(<%=article.getId()%>)">取消推荐
                                </button>
                            </td>
                            <%
                            } else {
                            %>
                            <td>
                                <button class="btn btn-primary" onclick="commandBlog(<%=article.getId()%>)">推荐</button>
                            </td>
                            <%
                                }
                            %>
                            <td>
                                <button type="button"
                                        onclick="getArticleModel('<%=article.getTitle()%>','<%=article.getUser()%>','<%=article.getId()%>','<%=article.getTime()%>',
                                                '<%=article.getType()%>','<%=article.getSubfield()%>','<%=article.getCategory()%>','<%=article.getViews()%>','<%=article.getCommentCount()%>','<%=article.getLikingDegree()%>','<%=article.getLike()%>')"
                                        class="btn btn-info">
                                    编辑
                                </button>
                            </td>
                            <td>
                                <button class=" btn btn-danger" onclick="deleteBlog(<%=article.getId()%>)">删除</button>
                            </td>
                            <%
                                if (article.getApprove() == 1) {
                            %>
                            <td>
                                <button class="btn btn-success">已审核</button>
                            </td>
                            <%
                            } else {
                            %>
                            <td>
                                <button class="btn btn-info" onclick="checkBlog(<%=article.getId()%>)">审核</button>
                            </td>
                            <%
                                }
                            %>
                        </tr>
                        <%
                            }
                        %>
                        <div class="modal fade" tabindex="-1" role="dialog" id="articleModal"
                             aria-labelledby="gridSystemModalLabel">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="gridSystemModalLabel1">修改博客信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <span>标题</span><input id="a1" class="form-control">
                                        <span>作者</span><input id="a2" class="form-control" readonly>
                                        <span>账号</span><input id="a3" class="form-control" readonly>
                                        <span>时间</span><input id="a4" class="form-control">
                                        <span>类型</span><input id="a5" class="form-control">
                                        <span>分栏</span><input id="a6" class="form-control">
                                        <span>分类</span><input id="a7" class="form-control">
                                        <span>浏览数</span><input id="a8" class="form-control">
                                        <span>评论数</span><input id="a9" class="form-control">
                                        <span>喜爱度</span><input id="a10" class="form-control">
                                        <span>点赞数</span><input id="a11" class="form-control">
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                        <button type="button" class="btn btn-primary" onclick="updateBlog()">保存修改
                                        </button>
                                    </div>
                                </div><!-- /.modal-content -->
                            </div><!-- /.modal-dialog -->
                        </div><!-- /.modal -->
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    //切换面板
    let divs = ["users", "articles"];

    function switchPanel(div, li) {
        document.getElementById(div).className = "show";//展示面板
        document.getElementsByClassName("list-group-item")[li].style.backgroundColor = "#e83737";
        for (let i = 0; i < divs.length; i++) {
            if (div !== divs[i]) {
                document.getElementById(divs[i]).className = "hidden";//隐藏面板
            }
        }
        for (let i = 0; i < 2; i++) {
            if (i !== li) {
                document.getElementsByClassName("list-group-item")[i].style.backgroundColor = "white";
            }
        }
    }

    //删除博客
    function deleteBlog(account) {
        $.ajax(
            {
                data: {'deleteAccount': account, "action": "deleteBlog"},
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    alert("删除成功");
                    window.location = "<%=basePath%>administratorPage.jsp";
                },
                error: function () {
                    alert("删除失败");
                }
            }
        );
    }

    function checkBlog(id) {
        $.ajax(
            {
                data: {'checkAccount': id, "action": "checkBlog"},
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    alert("审核成功");
                    window.location = "<%=basePath%>administratorPage.jsp";
                },
                error: function () {
                    alert("审核失败");
                }
            }
        );
    }

    //编辑博客
    function updateBlog() {
        let title = document.getElementById('a1').value;
        let user = document.getElementById('a2').value;
        let account = document.getElementById('a3').value;
        let time = document.getElementById('a4').value;
        let type = document.getElementById('a5').value;
        let subfield = document.getElementById('a6').value;
        let category = document.getElementById('a7').value;
        let views = document.getElementById('a8').value;
        let commentCount = document.getElementById('a9').value;
        let likingDegree = document.getElementById('a10').value;
        let like = document.getElementById('a11').value;
        $.ajax(
            {
                data: {
                    "updateTitle": title,
                    "updateUser": user,
                    'updateAccount': account,
                    "updateTime": time,
                    "updateType": type,
                    "updateSubfield": subfield,
                    "updateViews": views
                    ,
                    "updateCommentCount": commentCount,
                    "updateLikingDegree": likingDegree,
                    "updateCategory": category,
                    "updateLike": like,
                    "action": "updateBlog"
                },
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    window.location = "<%=basePath%>administratorPage.jsp";
                },
                error: function () {
                    alert("编辑失败");
                }
            }
        );
    }

    //浏览博客
    function browseBlog(account) {
        $.ajax(
            {
                data: {'browseAccount': account, "action": "browseBlog"},
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    window.location = "<%=basePath%>blogPage.jsp";
                },
                error: function () {
                    alert("查看失败");
                }
            }
        );
    }

    //推荐博客
    function commandBlog(account) {
        $.ajax(
            {
                data: {'commandAccount': account, "action": "commandBlog"},
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    window.location = "<%=basePath%>administratorPage.jsp";
                },
                error: function () {
                    alert("推荐失败");
                }
            }
        );
    }

    //删除用户
    function deleteUser(account) {
        $.ajax(
            {
                data: {'deleteAccount': account, "action": "deleteUser"},
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    alert("删除成功");
                    window.location = "<%=basePath%>administratorPage.jsp";
                },
                error: function () {
                    alert("删除失败");
                }
            }
        );
    }

    //编辑用户
    function updateUser() {
        let account = document.getElementById("u1").value;
        let psw = document.getElementById("u2").value;
        let name = document.getElementById("u3").value;
        let sex = document.getElementById("u4").value;
        let age = document.getElementById("u5").value;
        let sign = document.getElementById("u6").value;
        let screenName = document.getElementById("u7").value;
        let birthday = document.getElementById("u8").value;
        let phone = document.getElementById("u9").value;
        let email = document.getElementById("u10").value;
        let address = document.getElementById("u11").value;
        $.ajax(
            {
                data: {
                    'updateAccount': account,
                    "updatePsw": psw,
                    "updateName": name,
                    "updateSex": sex,
                    "updateSign": sign,
                    "updateScreenName": screenName,
                    "updateBirthday": birthday,
                    "updatePhone": phone,
                    "updateEmail": email,
                    "updateAddress": address,
                    "updateAge": age,
                    "action": "updateUser"
                },
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    window.location = "<%=basePath%>administratorPage.jsp";
                },
                error: function () {
                    alert("修改失败");
                }
            }
        );
    }

    function getArticleModel(title, user, account, time, type, subfield, category, views, commentCount, likingDegree, like) {
        document.getElementById('a1').value = title;
        document.getElementById('a2').value = user;
        document.getElementById('a3').value = account;
        document.getElementById('a4').value = time;
        document.getElementById('a5').value = type;
        document.getElementById('a6').value = subfield;
        document.getElementById('a7').value = category;
        document.getElementById('a8').value = views;
        document.getElementById('a9').value = commentCount;
        document.getElementById('a10').value = likingDegree;
        document.getElementById('a11').value = like;
        $('#articleModal').modal('show');
    }

    function getUserModal(headPortrait, account, psw, name, sex, sign, screenName, birthday, phone, email, address, age) {
        document.getElementById("m1").src = headPortrait;
        document.getElementById("u1").value = account;
        document.getElementById("u2").value = psw;
        document.getElementById("u3").value = name;
        document.getElementById("u4").value = sex;
        document.getElementById("u5").value = age;
        document.getElementById("u6").value = sign;
        document.getElementById("u7").value = screenName
        document.getElementById("u8").value = birthday
        document.getElementById("u9").value = phone;
        document.getElementById("u10").value = email;
        document.getElementById("u11").value = address;
        $('#userModel').modal('show');
    }

    function cancelCommand(id) {
        $.ajax(
            {
                data: {'deleteAccount': id, "action": "deleteCommand"},
                dataType: 'text',
                type: 'post',
                url: '<%=basePath%>ManagementServlet',
                success: function () {
                    alert("删除成功");
                    window.location = "<%=basePath%>administratorPage.jsp";
                },
                error: function () {
                    alert("删除失败");
                }
            }
        );
    }
</script>
</body>
</html>
